//
//  PersonalCenterVc.m
//  BMProject
//
//  Created by 段大志 on 16/1/6.
//  Copyright (c) 2016 BMProject. All rights reserved.
//

#import "PersonalCenterVc.h"
#import "UIViewController+common.h"
#import "DataModel.h"
#import "UserInfoCell.h"
#import "LogoutView.h"
#import "UIView+Extension.h"

@interface PersonalCenterVc () <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UserModel *userModel;
@property (strong, nonatomic) NSMutableArray *griddingAreas;//网格数组
@end

@implementation PersonalCenterVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.extendedLayoutIncludesOpaqueBars =YES;
    self.modalPresentationCapturesStatusBarAppearance =YES;
    self.navigationController.navigationBar.translucent =YES;


    self.title = @"个人中心";
    NAV_INIT(self, @"个人中心", @"back", @selector(backButtonClick:), @"", nil);

    self.userModel = [[AppDelegate appDelegate] g_UserInfo];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, UIScreenWidth, UIScreenHeight)) style:UITableViewStyleGrouped];
    }
    return _tableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)logoutAction:(id)sender {
        [[AppDelegate appDelegate] toLogin];
        [[AppDelegate appDelegate] setG_UserInfo:nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        self.griddingAreas = _userModel.griddingArea;
        return self.griddingAreas == nil ? 0 : self.griddingAreas.count;
    }
    return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 1) {
        LogoutView *view1 = [[[UINib nibWithNibName:@"LogoutView" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];
        view1.clipsToBounds = YES;
        [view1.outBtn addTarget:self action:@selector(logoutAction:) forControlEvents:UIControlEventTouchUpInside];
        return view1;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return 75;
    }
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UserInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UserInfoCell"];
    if (!cell)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"UserInfoCell" owner:self options:nil]lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    if (indexPath.section == 0) {
        SysUser *sysUser = self.userModel.sysUser;
        if (indexPath.row == 0) {//姓名
            cell.titleLa.font = [UIFont systemFontOfSize:17];
            cell.titleLa.textColor = [UIColor blackColor];
            cell.titleLa.text = [NSString ignoreEmptyStr:sysUser.userName];
        } else if (indexPath.row == 1) {//职务
            cell.titleLa.font = [UIFont systemFontOfSize:14];
            cell.titleLa.textColor = [UIColor lightGrayColor];
            cell.titleLa.text = [NSString ignoreEmptyStr:sysUser.userTypeMyname];
        } else if (indexPath.row == 2) {//分公司信息
            SysUnit *sysUnit = self.userModel.sysUnit;
            cell.titleLa.font = [UIFont systemFontOfSize:14];
            cell.titleLa.textColor = [UIColor lightGrayColor];
            cell.titleLa.text = [NSString ignoreEmptyStr:sysUnit.name];
        }
    } else {
        cell.titleLa.font = [UIFont systemFontOfSize:12];
        cell.titleLa.textColor = [UIColor lightGrayColor];
        self.griddingAreas = _userModel.griddingArea;
        NSArray *array = self.griddingAreas[indexPath.row];
        NSString *addressStr = array[0];
        if (indexPath.row < self.griddingAreas.count) {
            cell.titleLa.text = [NSString ignoreEmptyStr:addressStr];
        }
    }

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == 0)  {
        [[AppDelegate appDelegate] toLogin];
        [[AppDelegate appDelegate] setG_UserInfo:nil];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
